using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace UTLBASEFORM
{
    public class ImageDimension
    {

        //Generate new image dimensions

        public static Size GenerateImageDimensions(int currW, int currH, int destW, int destH)
        {

            //double to hold the final multiplier to use when scaling the image

            double multiplier = 0;



            //string for holding layout

            string layout;



            //determine if it's Portrait or Landscape

            if (currH > currW) layout = "portrait";

            else layout = "landscape";



            switch (layout.ToLower())
            {

                case "portrait":

                    //calculate multiplier on heights

                    if (destH > destW)
                    {

                        multiplier = (double)destW / (double)currW;

                    }
                    else
                    {
                        multiplier = (double)destH / (double)currH;
                    }

                    break;

                case "landscape":

                    //calculate multiplier on widths

                    if (destH > destW)
                    {
                        multiplier = (double)destW / (double)currW;
                    }
                    else
                    {
                        multiplier = (double)destH / (double)currH;
                    }

                    break;
            }



            //return the new image dimensions

            return new Size((int)(currW * multiplier), (int)(currH * multiplier));
        }



        //Resize the image

        public static void SetImage(PictureBox pb)
        {
            try
            {

                //create a temp image

                Image img = pb.Image;

                //calculate the size of the image

                Size imgSize = GenerateImageDimensions(img.Width, img.Height, pb.Width, pb.Height);

                //create a new Bitmap with the proper dimensions

                Bitmap finalImg = new Bitmap(img, imgSize.Width, imgSize.Height);

                //create a new Graphics object from the image

                Graphics gfx = Graphics.FromImage(img);

                //clean up the image (take care of any image loss from resizing)

                gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;

                //empty the PictureBox

                pb.Image = null;
                
                //center the new image

                pb.SizeMode = PictureBoxSizeMode.CenterImage;

                //set the new image

                pb.Image = finalImg;
            }

            catch (System.Exception e)
            {

                MessageBox.Show(e.Message);

            }

        }
    }
}
